package users

import (
	"errors"
	v1 "ginx/api/users/v1"
	"ginx/internal/dao"

	"gorm.io/gorm"
)

func (u *Users) QueryById(id int64) (res *v1.QueryRes, err error) {
	result, err := dao.User.Where(dao.User.ID.Eq(id)).First()
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			err = errors.New("用户不存在")
		}
		return nil, err
	}
	response := &v1.QueryRes{
		Id:       result.ID,
		Username: result.Username,
		Email:    result.Email,
	}
	return response, err
}
